# Alexander F. Gazmararian
# afg2@princeton.edu

# Load packages
library(tidyverse)
library(modelsummary)

# Load data
g <- readRDS("data/NatYouthQual.rds")

# Make names of covariates pretty
g$income5 <- gsub("[$]", "", g$income5)
g$Dem <- ifelse(g$PartySummary == "Democrat", 1, 0)
g$Rep <- ifelse(g$PartySummary == "Republican", 1, 0)

# Get summary statistics for Youth
file <- "tables/tbl_summarystat_youth.txt"
datasummary(
  formula = (Age = age) + (Female)  + Black + Hispanic + (College = CollegeDegree) +
    (Democrat = Dem) + (Republican = Rep) + (Liberal = Lib) + (Conservative = Con) + (Income = income5) ~ Mean + SD + N + Percent(),
  data = subset(g, youth_block == "Youth"),
  fmt = 2,
  escape = FALSE,
  format = "latex"
) %>%
  cat(., file = file)
# Trim table shell
tbl <- readLines(file)
tbl <- tbl[-c(1,2,length(tbl))]
cat(tbl, file = file)

# Get summary statistics for Non-Youth Block
file <- "tables/tbl_summarystat_youthAdult.txt"
datasummary(
  formula = (Age = age) + (Female)  + Black + Hispanic + (College = CollegeDegree) +
    (Democrat = Dem) + (Republican = Rep) + (Liberal = Lib) + (Conservative = Con) + (Income = income5) ~ Mean + SD + N + Percent(),
  data = subset(g, youth_block == "Non-Youth"),
  fmt = 2,
  escape = FALSE,
  format = "latex"
) %>%
  cat(., file = file)
# Trim table shell
tbl <- readLines(file, warn = FALSE)
tbl <- tbl[-c(1,2,length(tbl))]
cat(tbl, file = file)
